<?php
namespace Home\Controller;
use Common\Controller\HomeController;
class PersoncenterController extends HomeController {

	public function _initialize(){
		parent::_initialize();
		if (!empty($session_id)) {
			$this->session_name=$session_name;
			$this->session_id=$session_id;
			$this->comnum=M('Shopcar')->where('user_id='.session('id'))->count();
			$this->shopCar=M('Shopcar')->where('user_id='.session('id'))->select();
		}
		$cate=M('Category')->select();
        $this->commo=M('Commodity')->join(" fm_com_image ON fm_com_image.com_id=fm_commodity.id ")->select();
        $this->assign('productshow',U('Home/Product/product_show'));
        $this->cates=getCategoryPid(0);
		if(!empty($session_name)){
			$name=session('name');
			$id=M('HomeUser')->field('id')->where("username='{$name}'")->find();
			$id=$id['id'];
			//取出头像
			$photo=M('HomeUserPhoto')->where("home_user_id='{$id}'")->select();
			// print_r($photo);
			$this->time=date("Y-m-d",$photo[0]['create_time']);
			$this->assign('photo',$photo[0]['photo_name']);
			$this->assign('name',$name);
		}
		$this->assign('orderall',U('Home/Personcenter/orderall'));
		$this->member=U('Home/Personcenter/member');
		$this->addaddress=U('Home/Personcenter/addaddress');
	}
	public function member(){
		$session_name=session('name');
		if(!empty($session_name)){
			
			$name=session('name');
			$id=M('HomeUser')->field('id')->where("username='{$name}'")->find();
			$id=$id['id'];
			$this->userinfo=M('HomeUser')->where("id=$id")->select();
		}
		// echo $this->userinfo[0]['username'];
		// exit;
		
		$this->assign('upuser',U('Home/Personcenter/upuser'));
		$this->assign('upload',U('Home/Personcenter/upload'));
		
		$this->display();
	}
	public function upuser(){
		if(!empty($session_name)){
			$name=session('name');
			$id=M('HomeUser')->field('id')->where("username='{$name}'")->find();
			$id=$id['id'];
		}
		if (IS_POST) {
			$mail=I('post.email');
			$prepass=I('post.prepass');
			$mypass=md5($prepass);
			$email=M('HomeUser')->field('mail,password')->where('id='.session('id'))->find();
			if ($mail==$email['mail']&&$mypass==$email['password']) {
				$data=array(
					'username'=>I('post.username'),
					'mail'=>I('post.email'),
					'password'=>md5(I('post.newpass')),
					'add_time'=>time(),
					);
				$save=M('HomeUser')->where('id='.session('id'))->save($data);
				if ($save) {
					$this->success('请重新登录！',U("Home/Index/login"));
				}else{
					$this->error(M('HomeUser')->getError('更新失败！'));
				}
			}else{
				$this->error(M('HomeUser')->getError('更新失败！'));
			}
		}
	}
	public function addaddress(){
		//找出所有的地址
		parent::_initialize();
		if(!empty($session_name)){
			$name=session('name');
			$id=M('HomeUser')->field('id')->where("username='{$name}'")->find();
			$id=$id['id'];
			$addresses=M('UserAddress')->where('user_id='.$id)->order('id desc')->select();
			if ($addresses) {
				$this->assign('addresses',$addresses);
			}
		}
		$this->session_name=$session_name;
		//找出地区的联动下拉框的值
		$regionmodel=M('region');
		$this->states=$regionmodel->where("pid=1")->select();
		$this->areas=$regionmodel->where("pid=2")->select();
		$this->locals=$regionmodel->where("pid=33")->select();
		$this->region=U('Home/Personcenter/region');
		$this->addressact=U('Home/Personcenter/addressact');
		$this->del=U('Home/Personcenter/delete');
		
		$this->display();
	}
	public function delete(){
		if (IS_GET) {
			$del=M('UserAddress')->where('id='.$_GET['id'])->delete();
			if ($del) {
				$this->success('删除成功！',U('Home/Personcenter/addaddress'));
			}else{
				$this->error('删除失败！');
			}
		}
	}
	public function addressact(){
		
		if(!empty($session_name)){
			$name=session('name');
			$id=$session_id;
			$id=M('HomeUser')->field('id')->where("username='{$name}'")->find();
			$id=$id['id'];
		}
		$add1=I('post.add1');
		$add1=M('region')->field('name')->where('id='.$add1)->find();
		$add2=I('post.add2');
		$add2=M('region')->field('name')->where('id='.$add2)->find();
		$add3=I('post.add3');
		$add3=M('region')->field('name')->where('id='.$add3)->find();
		$add4=I('post.add4');
		$add=$add1['name'].$add2['name'].$add3['name'].$add4;
		$data=array(
			'address'=>$add,
			'username'=>I('post.username'),
			'email'=>I('post.email'),
			'postcode'=>I('post.postcode'),
			'gender'=>I('post.gender'),
			'phone'=>I('post.phone'),
			'tel'=>I('post.tel'),
			'user_id'=>$id,
			);
		$userAddress=D('UserAddress');
		if ($userAddress->create($data)) {
			if ($userAddress->add()) {
                $this->success('添加成功！',U('Home/Personcenter/addaddress'));
            }else{
                $this->error('添加失败！');
            }
		}else{
			$this->error('添加失败！');
		}
		print_r($data);
		exit;
		$this->display();
	}
	public function region(){
		if (IS_AJAX) {
			$pid=I('post.pid',0,'intval');
			if ($pid>0) {
				$regionmodel=M('region');
				$data['status']="OK";
				$data['level']=I('post.level',0,'intval');
				$data['area']=$regionmodel->where('pid='.$pid)->select();
				$this->ajaxReturn($data);
			}
		}
	}

	public function upload(){
		
		if(!empty($session_name)){
			$name=session('name');
			$id=M('HomeUser')->field('id')->where("username='{$name}'")->find();
			$id=$id['id'];
		}
	    $upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     THINK_PATH; // 设置附件上传根目录
	    $upload->savePath  =     '../Public/Uploads/'; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->uploadOne($_FILES['photo']);
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功
	    	M('HomeUserPhoto')->where("home_user_id='{$id}'")->delete();
	    	$model = M('HomeUserPhoto');
			// 取得成功上传的文件信息
			// $info = $upload->uploadOne($_FILES['photo']);
			
			// 保存当前数据对象
			$data['photo_name'] = $info['savename'];
			$data['create_time'] = time();
			$data['home_user_id'] = $id;
			$model->add($data);
	        $this->success('上传成功！');
	    }	
	}

	public function orderall(){
		$order=M('Payorder')->where('status=0')->select();
		foreach ($order as $key => $value) {
			$comid[]=M('ComPayorder')->field('com_id')->where('payorder_id='.$value['id'])->select();
		}
		foreach ($comid as $key => $value) {
			foreach ($value as $k => $v) {
				$name=M('Commodity')->field('com_name')->where('id='.$v['com_id'])->find();
				$com[$key][$k]['com_name']=$name['com_name'];
			}
		}

		foreach ($com as $key => $value) {
			$str="";
			foreach ($value as $k => $v) {
				$str.=$v['com_name'].'/';
			}
			$str=substr($str,0,strlen($str)-1);
			$com[$key]=$str;
		}
		foreach ($order as $key => $value) {
			foreach ($com as $k => $v) {
				if ($key===$k) {
					$order[$key]['coms']=$v;
				}
			}
		}
		$this->assign('del',U('Home/Personcenter/del'));
		$this->assign('pay',U('Home/Shopcar/payply'));
		$this->assign('order',$order);
		$this->display();
	}

	public function del(){
		if (IS_GET) {
			$payid=I('get.payid',0,'intval');
			// $success=M('Payorder')->where('id='.$payid)->delete();
			// if ($success) {
			// 	$success=M('ComPayorder')->where('payorder_id='.$payid)->delete();
			// 	if ($success) {
			// 		$this->success('删除成功！',U("index.php/Home/Personcenter/orderall"));
			// 	}else{
			// 		$this->error('删除失败！');
			// 	}
			// }else{
			// 	$this->error('删除失败！');
			// }
			$data['status']='已取消';
			$code=M('Payorder')->where('id='.$payid)->save($data);
			if ($code) {
				$this->success('取消成功！',U("Home/Personcenter/orderall"));
			}else{
				$this->error('取消失败！');
			}
		}
	}
}